﻿using B.S.DataBase.MyProject.Domain.Infrastructures;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.OperationaldecisionsCommand;
using MedicalPCTerminal.Read.API.Dtos;
using SqlSugar;
using System.Collections.Generic;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.OperationaldecisionsHandler
{
    public class ManagementhierarchyQueryHandler : IRequestHandler<ManagementhierarchyQueryCommand, ApiPaging<ManagementhierarchyDto>>
    {

        /// <summary>
        /// sqlsugar
        /// </summary>

        private readonly SqlSugarContext sugar;

        public ManagementhierarchyQueryHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }

        public async Task<ApiPaging<ManagementhierarchyDto>> Handle(ManagementhierarchyQueryCommand request, CancellationToken cancellationToken)
        {

            ApiPaging<ManagementhierarchyDto> result = new ApiPaging<ManagementhierarchyDto>();
            var list = sugar.db.Queryable<NCDSign>()
                .LeftJoin<ResidentFiles>((a, b) => a.Patient_Id == b.ResidentFiles_Id) // 第一个左连接
                .LeftJoin<TeamInfo>((a, b, c) => c.TeamLeader == a.Doctor) // 第二个左连接
                .WhereIF(request.StartTime.HasValue, (a, b, c) => a.Sign_StartTime >= request.StartTime)
                .WhereIF(request.EndTime.HasValue, (a, b, c) => a.Sign_StartTime <= request.EndTime)
                .WhereIF(!string.IsNullOrEmpty(request.OrgName), (a, b, c) => c.OrgName.Contains(request.OrgName))
                .Select((a, b, c) => new ManagementhierarchyDto
                {
                    Patient_Id = a.Patient_Id,
                    OrgName = c.OrgName,
                    Doctor = a.Doctor,
                    TeamLeader = c.TeamLeader,
                    Hypertension = b.Hypertension,
                    Diabetes = b.Diabetes,
                    BMI = b.BMI,
                    ResidentFiles_Id = b.ResidentFiles_Id,
                    Sign_StartTime = a.Sign_StartTime,
                    Sex = b.Sex,
                //    OrgPatientCount = SqlFunc.Subqueryable<NCDSign>().LeftJoin<ResidentFiles>((a, b) => a.Patient_Id == b.ResidentFiles_Id).Count()
                }).ToList();
                if (request.Hypertension != null  )
                {
                    list = list.Where(o => o.Hypertension==request.Hypertension).ToList();
                }
                if (request.Diabetes != null)
                {
                    list = list.Where(o => o.Diabetes==request.Diabetes).ToList();
                }
                result.TotalCount = list.Count();
                result.TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
                list = list.OrderByDescending(x => x.Patient_Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.list = list.ToList();
                return await Task.FromResult(result);



        }
    }
}
